/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/**
 * @file channel_mixing.cpp
 */
#include "channel_mixing_tiling.h"
#include "register/op_def_registry.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include "tiling/tiling_api.h"

using namespace matmul_tiling;

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    ChannelMixingTilingData tiling;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto x_shape = context->GetInputShape(0)->GetStorageShape();

    int32_t B = x_shape.GetDim(0);
    int32_t T = x_shape.GetDim(1);
    constexpr int32_t C = 2560;

    size_t userWorkspaceSize = 0;


    if (ascendcPlatform.GetSocVersion() == platform_ascendc::SocVersion::ASCEND310P) 
    {
        context->SetTilingKey(1);

        int32_t M = T;
        int32_t N = 4 * C;
        int32_t K = C;
        int32_t baseM = -1;
        int32_t baseN = -1;
        int32_t baseK = -1;
        if(T <= 16)
        {
            baseM = 16;
            baseN = 256;
            baseK = -1;
        }
        else if(T >= 128)
        {
            baseM = 128;
            baseN = 256;
            baseK = -1;
        }
        // else if(T >= 128)
        // {
        //     baseM = 256;
        //     baseN = 128;
        //     baseK = -1;
        // }

        MultiCoreMatmulTiling cubeTiling1(ascendcPlatform);
        cubeTiling1.SetDim(8);
        cubeTiling1.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling1.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16, true);
        cubeTiling1.SetCType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        // cubeTiling1.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT);
        cubeTiling1.SetShape(M, N, K);
        cubeTiling1.SetSingleShape(-1, -1, -1);
        cubeTiling1.SetOrgShape(M, N, K);
        cubeTiling1.SetFixSplit(baseM, baseN, baseK);
        cubeTiling1.EnableBias(false);
        cubeTiling1.SetBufferSpace(-1, -1, -1, -1);
        // cubeTiling1.SetMatmulConfigParams({1, false, ScheduleType::OUTER_PRODUCT, MatrixTraverse::FIRSTM});
        if (cubeTiling1.GetTiling(tiling.cubeTilingData1) == -1) { // Get matmul tiling.
            return ge::GRAPH_FAILED;
        }
        if(T <= 16)
        {
            baseM = -1;
            baseN = 64;
            baseK = -1;
        }

        // if(T <= 16)
        // {
        //     baseM = -1;
        //     baseN = 128;
        //     baseK = -1;
        // }

        M = T;
        N = C;
        K = 4 * C;
        MultiCoreMatmulTiling cubeTiling2(ascendcPlatform);
        cubeTiling2.SetDim(8);
        cubeTiling2.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling2.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16, true);
        cubeTiling2.SetCType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        // cubeTiling2.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT);
        cubeTiling2.SetShape(M, N, K);
        cubeTiling2.SetSingleShape(-1, -1, -1);
        cubeTiling2.SetOrgShape(M, N, K);
        cubeTiling2.SetFixSplit(baseM, baseN, baseK);
        cubeTiling2.EnableBias(false);
        cubeTiling2.SetBufferSpace(-1, -1, -1, -1);
        // cubeTiling2.SetMatmulConfigParams({1, false, ScheduleType::OUTER_PRODUCT, MatrixTraverse::FIRSTM});
        if (cubeTiling2.GetTiling(tiling.cubeTilingData2) == -1) { // Get matmul tiling.
            return ge::GRAPH_FAILED;
        }

        tiling.set_B(B);
        tiling.set_T(T);
        // printf(" !%d %d %d! ",cubeTiling1.GetBaseM(), cubeTiling1.GetBaseN(), cubeTiling1.GetBaseK());
        // printf(" !%d %d %d! ",cubeTiling2.GetBaseM(), cubeTiling2.GetBaseN(), cubeTiling2.GetBaseK());

        // int32_t singleM, singleN, singleK;
        // cubeTiling1.GetSingleShape(singleM, singleN, singleK);  
        // printf(" @%d %d %d@ ",singleM, singleN, singleK);

        // cubeTiling2.GetSingleShape(singleM, singleN, singleK);  
        // printf(" @%d %d %d@ ",singleM, singleN, singleK);

        context->SetBlockDim(8);
        userWorkspaceSize = 8*32;//(m1*N*4 + m2*N*4)*2;
    } 
    else 
    {
        context->SetTilingKey(2);

        int32_t M = T;
        int32_t N = 4 * C;
        int32_t K = C;
        int32_t baseM = -1;
        int32_t baseN = -1;
        int32_t baseK = -1;
        if(T <= 16)
        {
            baseM = -1;
            baseN = 64;
            baseK = 512;
        }
        else if(T >= 128)
        {
            baseM = -1;
            baseN = 256;
            baseK = 64;
        }
        MatmulApiTiling cubeTiling1(ascendcPlatform);
        cubeTiling1.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling1.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16, true);
        cubeTiling1.SetCType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling1.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling1.SetShape(M, N, K);
        cubeTiling1.SetOrgShape(M, N, K);
        cubeTiling1.SetFixSplit(baseM, baseN, baseK);
        cubeTiling1.EnableBias(false);
        cubeTiling1.SetBufferSpace(-1, -1, -1);
        if (cubeTiling1.GetTiling(tiling.cubeTilingData1) == -1) { // Get matmul tiling.
            return ge::GRAPH_FAILED;
        }

        M = T;
        N = C;
        K = 4 * C;
        MatmulApiTiling cubeTiling2(ascendcPlatform);
        cubeTiling2.SetAType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling2.SetBType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16, true);
        cubeTiling2.SetCType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling2.SetBiasType(TPosition::GM, CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
        cubeTiling2.SetShape(M, N, K);
        cubeTiling2.SetOrgShape(M, N, K);
        cubeTiling2.SetFixSplit(baseM, baseN, baseK);
        cubeTiling2.EnableBias(false);
        cubeTiling2.SetBufferSpace(-1, -1, -1);
        if (cubeTiling2.GetTiling(tiling.cubeTilingData2) == -1) { // Get matmul tiling.
            return ge::GRAPH_FAILED;
        }
        context->SetBlockDim(1);
        userWorkspaceSize = 0;
    }

    tiling.set_B(B);
    tiling.set_T(T);

    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t systemWorkspaceSize = static_cast<size_t>(ascendcPlatform.GetLibApiWorkSpaceSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = userWorkspaceSize + systemWorkspaceSize;
    return ge::GRAPH_SUCCESS;
}
}

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static graphStatus InferDataType(gert::InferDataTypeContext* context)
{
    const auto inputDataType = context->GetInputDataType(0);
    context->SetOutputDataType(0, inputDataType);
    return ge::GRAPH_SUCCESS;
}
}

namespace ops {
class ChannelMixing : public OpDef {
public:
    explicit ChannelMixing(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Input("h0")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Input("xk")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Input("kw")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Input("vw")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        
        this->Output("out")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Output("ht")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Output("hs")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);
        this->AICore()
            .SetTiling(optiling::TilingFunc)
            .AddConfig("ascend310p")
            .AddConfig("ascend310b");
    }
};
OP_ADD(ChannelMixing);
}

